define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    Fast.config.openArea = ['70%', '80%'];
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ldcms/category/index' + location.search,
                    add_url: 'ldcms/category/add',
                    edit_url: 'ldcms/category/edit',
                    del_url: 'ldcms/category/del',
                    multi_url: 'ldcms/category/multi',
                    import_url: 'ldcms/category/import',
                    table: 'ldcms_category',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                pagination: false,
                escape: false,
                fixedRightNumber: 1,
                commonSearch:false,
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id',title:__('Id'),operate: false,width: 60},
                        {
                            field: 'id',
                            title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle"><i class="fa fa-chevron-up"></i></a>',
                            operate: false,
                            width:60,
                            formatter: Controller.api.formatter.subnode
                        },
                        {field: 'name', title: __('Name'), align: 'left'},
                        {field: 'urlname', title: __('Urlname'),width: 180},
                        {field: 'mid_text', title: __('Mid')+'/类型',width: 100,formatter:function (value,row,index){
                            if(row['type']==0){
                                return '<span class="label label-warning">'+value+'</span>';
                            }
                            return '<span class="label label-primary">链接</span>';
                        }},
                        {field: 'outlink', width:200, title: __('Outlink'), operate: false},
                        {field: 'template_list', width:200, title: __('Template_list'), operate: false},
                        {field: 'template_detail', width:200, title: __('Template_detail'), operate: false},
                        {field: 'is_nav', width:80,  title: __('Is_nav'), searchList: {"1": __('Yes'), "0": __('No')}, formatter: Table.api.formatter.toggle},
                        {field: 'status', width:80,  title: __('Status'),searchList: {"1": __('Yes'), "0": __('No')}, formatter: Table.api.formatter.toggle },
                        {field: 'gid', width:100,  title: __('Gid'), formatter: function (value,row,index) {
                            if(value==0){
                                return '公开';
                            }
                            var arr=[];
                            value.split(',').forEach(function (item){
                                arr.push('会员等级'+item)
                            })
                            return arr.join(',')
                        }},
                        {field: 'sort', title: __('Sort'),width: 80,operate: false,formatter: Controller.api.formatter.input,events: {"dblclick .text-sort": function (e) {
                            e.preventDefault();
                            e.stopPropagation();
                            return false;
                        }}},
                        {field: 'create_time',width:140,  title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            //当内容渲染完成后
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                // //默认隐藏所有子节点
                // $("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").hide();
                // $(".btn-node-sub.disabled[data-pid!=0]").closest("tr").hide();

                //显示隐藏子节点
                $(".btn-node-sub").off("click").on("click", function (e) {
                    var status = $(this).data("shown") || $("a.btn[data-pid='" + $(this).data("id") + "']:visible").size() > 0 ? true : false;
                    $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
                        $(this).closest("tr").toggle(!status);
                        if (!$(this).hasClass("disabled")) {
                            $(this).trigger("click");
                        }
                    });
                    $(this).data("shown", !status);
                    return false;
                });
            });

            //展开隐藏一级
            $(document.body).on("click", ".btn-toggle", function (e) {
                $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
                var that = this;
                var show = $("i", that).hasClass("fa-chevron-down");
                $("i", that).toggleClass("fa-chevron-down", !show);
                $("i", that).toggleClass("fa-chevron-up", show);
                $("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").toggle(show);
                $(".btn-node-sub[data-pid=0]").data("shown", show);
            });
            // //展开隐藏全部
            // $(document.body).on("click", ".btn-toggle-all", function (e) {
            //     var that = this;
            //     var show = $("i", that).hasClass("fa-plus");
            //     $("i", that).toggleClass("fa-plus", !show);
            //     $("i", that).toggleClass("fa-minus", show);
            //     $(".btn-node-sub.disabled[data-pid!=0]").closest("tr").toggle(show);
            //     $(".btn-node-sub[data-pid!=0]").data("shown", show);
            // });


            $(document).on("change", ".text-sort", function () {
                $(this).data("params", {sort: $(this).val()});
                Table.api.multi('', [$(this).data("id")], table, this);
                return false;
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter:{
                input: function (value, row, index) {
                    value = value == null || value.length === 0 ? '' : value.toString();
                    return '<input type="text" class="form-control input-sm text-sort" data-id="'+row.id+'" value="' + value + '">';
                },
                subnode: function (value, row, index) {
                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '
                        + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-' + (row.haschild == 1 || row.ismenu == 1 ? 'sitemap' : 'list') + '"></i></a>';
                }
            },
            bindevent: function () {
                var models={};
                $.get('ldcms/category/getModels',function (res){
                    models=res
                })

                $(document).on('change','#c-mid',function (){
                    var mid=$(this).val()
                    if(models[mid]['template_list'])
                        $('#c-template_list').val(models[mid]['template_list']).selectPageRefresh()
                    else
                        $('#c-template_list').selectPageClear()
                    $('#c-template_detail').val(models[mid]['template_detail']).selectPageRefresh()
                })

                //获取栏目拼音
                var si;
                $(document).on("keyup", "#c-name", function () {
                    var value = $(this).val();
                    if (value != '' && !value.match(/\n/)) {
                        clearTimeout(si);
                        si = setTimeout(function () {
                            Fast.api.ajax({
                                loading: false,
                                url: "ldcms/category/getTitlePinyin",
                                data: {title: value}
                            }, function (data, ret) {
                                $("#c-urlname").val(data.pinyin.substr(0, 100));
                                return false;
                            }, function (data, ret) {
                                return false;
                            });
                        }, 200);
                    }
                });

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
